/*
 * @Descripttion: 钉钉获取微应用免登授权码
 * @version: https://open.dingtalk.com/tools/explorer/jsapi?id=10295
 * @version: 
 * @Author: ada.wu
 * @Date: 2024-01-05 10:17:42
 * @LastEditors: ada.wu
 * @LastEditTime: 2024-04-10 09:51:14
 */

import * as dd from 'dingtalk-jsapi'; // 此方式为整体加载，也可按需进行加载
import { querySwitch } from '/@/api/NonmotorApi'; // 此方式为整体加载，也可按需进行加载
// src/api/NonmotorApi.ts
/**
 * 调用getAuthCode，获取应用免登授权码。
 * @param appId
 * @param redirectUrl
 * @param state
 */

export async function requestAuthCode() {
    return new Promise(async (resolve, reject) => {

        querySwitch({ paraId: "DINGTALK_CORP_ID", TYPE: "FORM" }).then(corpId => {
            dd.runtime.permission.requestAuthCode({
                // ding8f1d13a4705aaaf1bc961a6cb783455b 测试 刘光建
                // corpId: "ding51234ce536095b6635c2f4657eb6378f",//生产李娇
                // corpId: "ding8f1d13a4705aaaf1bc961a6cb783455b",
                corpId: corpId,
                onSuccess: function (result: any) {
                    const { code } = result;
                    resolve(code)
                    /*{
                        code: 'hYLK98jkf0m' //string authCode
                    }*/
                },
                onFail: function (err) {
                    console.log(err)
                    reject()
                }

            })
        }).catch(() => {
            reject()
        })


        // let corpId = await querySwitch({ paraId: "DINGTALK_CORP_ID", TYPE: "FORM" }).then
        // if (corpId) {
        //     console.log('corpId==', corpId)
        //     dd.runtime.permission.requestAuthCode({
        //         // ding8f1d13a4705aaaf1bc961a6cb783455b 测试 刘光建
        //         // corpId: "ding51234ce536095b6635c2f4657eb6378f",//生产李娇
        //         // corpId: "ding8f1d13a4705aaaf1bc961a6cb783455b",
        //         corpId: corpId,
        //         onSuccess: function (result: any) {
        //             const { code } = result;
        //             resolve(code)
        //             /*{
        //                 code: 'hYLK98jkf0m' //string authCode
        //             }*/
        //         },
        //         onFail: function (err) {
        //             console.log(err)
        //             reject()
        //         }

        //     })
        // }
        // dd.getAuthCode({
        //     corpId: 'ding51234ce536095b6635c2f4657eb6378f',
        //     success: (res: any) => {
        //         const { code } = res;
        //         resolve(code)
        //     },
        //     fail: () => {
        //         reject()
        //     },
        //     complete: () => { },
        // });
    })
}
